package cn.edu.hhu.DAO.impl;

import cn.edu.hhu.Bean.AttendanceInfo;
import cn.edu.hhu.DAO.AttendanceInfoDao;
import cn.edu.hhu.util.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.servlet.jsp.tagext.TryCatchFinally;
import java.sql.Timestamp;

/**
 * @anthor: lddd
 * @date: 2020/5/10 - 14:58
 * @description:
 **/
public class AttendanceInfoDaoImpl implements AttendanceInfoDao {
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 添加一条签到信息
     * @param attendanceInfo
     * @return 0表示失败，1表示成功
     */
    @Override
    public int insertAttendanceInfo(AttendanceInfo attendanceInfo) {
        try {
            String sql = "INSERT INTO attendance_info VALUES(?,?,?)";
            int update = template.update(sql, attendanceInfo.getTnum(), attendanceInfo.getSid(), attendanceInfo.getSign_in_time());
            return update;
        }catch (Exception e){
            return 0;
        }
    }

    /**
     * 通过学号和任务序号查询签到信息
     * @param tnum
     * @param sid
     * @return 签到信息
     */
    @Override
    public AttendanceInfo findByTnumAndSid(long tnum, String sid) {
        try {
            String sql = "SELECT * FROM attendance_info WHERE tnum = ? AND sid = ?";
            AttendanceInfo attendanceInfo = template.queryForObject(sql, new BeanPropertyRowMapper<>(AttendanceInfo.class), tnum, sid);
            return attendanceInfo;
        }
        catch (Exception e){
            return null;
        }
    }
}
